package com.rxt.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.rxt.common.constant.RedisConstants;
import com.rxt.common.result.Result;
import com.rxt.model.domain.FBankUser;
import com.rxt.mapper.FBankUserMapper;
import com.rxt.model.vo.BankUserLoginVo;
import com.rxt.model.vo.BankUserVo;
import com.rxt.model.vo.PasswordVo;
import com.rxt.service.IFBankUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rxt.util.BankThreadLocal;
import com.rxt.util.BaseContext;
import com.rxt.util.JwtUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 周梓文
 * @since 2023-08-24
 */
@Service
public class FBankUserServiceImpl extends ServiceImpl<FBankUserMapper, FBankUser> implements IFBankUserService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public Result login(BankUserLoginVo bankUserVo) {

        String username = bankUserVo.getUsername();
        String password = bankUserVo.getPassword();

        FBankUser one = this.getOne(new LambdaQueryWrapper<FBankUser>().eq(FBankUser::getUsername, username));
        if (one == null){
            return Result.fail("用户名或密码错误");
        }else if (!one.getPassword().equals(password)){
            return Result.fail("用户名或密码错误");
        }
        one.setPassword(null);

        String token = JwtUtils.createToken(one.getUsername());

        redisTemplate.opsForValue().set("BankUser_" + one.getUsername(), JSON.toJSONString(one),3, TimeUnit.HOURS);

        return Result.ok(token);
    }

    @Override
    public Result saveUser(BankUserVo bankUserVo) {
        if (bankUserVo == null){
            return Result.fail("信息有误");
        }

        FBankUser one = this.getOne(new LambdaQueryWrapper<FBankUser>().eq(FBankUser::getUsername, bankUserVo.getUsername()));
        if (one!=null){
            return Result.fail("用户名已存在");
        }
        FBankUser user = new FBankUser();
        BeanUtils.copyProperties(bankUserVo,user);

        this.save(user);
        user.setPassword(null);
        return Result.ok(user);
    }

    @Override
    public Result currentUser() {

        FBankUser fBankUser = BankThreadLocal.get();
        return Result.ok(fBankUser);

    }

    @Override
    public Result resetPwd(PasswordVo pwdVo) {

        FBankUser fBankUser = BankThreadLocal.get();
        String oldPassword = pwdVo.getPassword();
        String newPassword = pwdVo.getNewPassword();

        FBankUser one = this.getOne(new LambdaQueryWrapper<FBankUser>().eq(FBankUser::getId, fBankUser.getId()));
        if (!one.getPassword().equals(oldPassword)){
            return Result.fail("旧密码错误");
        }else{
            one.setPassword(newPassword);
            this.updateById(one);
            return Result.ok(fBankUser);
        }
    }

    @Override
    public Result updateUserInfo(BankUserVo bankUserVo) {

        FBankUser fBankUser = BankThreadLocal.get();

        if (bankUserVo == null){
            return Result.fail("信息有误");
        }
        if (bankUserVo.getBankId()!=null && bankUserVo.getBankId()!=0){
            fBankUser.setBankId(bankUserVo.getBankId());
        }
        if (StringUtils.isNotBlank(bankUserVo.getPhone())){
            fBankUser.setPhone(bankUserVo.getPhone());
        }
        if (StringUtils.isNotBlank(bankUserVo.getRole())){
            fBankUser.setRole(bankUserVo.getRole());
        }
        if (StringUtils.isNotBlank(bankUserVo.getRealName())){
            fBankUser.setRealName(bankUserVo.getRealName());
        }
        this.updateById(fBankUser);
        fBankUser.setPassword(null);

        redisTemplate.opsForValue().set("BankUser_" + fBankUser.getUsername(), JSON.toJSONString(fBankUser),3, TimeUnit.HOURS);
        return Result.ok(fBankUser);
    }

    @Override
    public Result logout() {
        // 删除redis中的用户信息
        redisTemplate.delete("BankUser_" + BankThreadLocal.get().getUsername());
        return Result.ok();
    }
}
